package com.scalar.forecast.salesPersonSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.salesPersonSetup.forms.SalesPersonSetupForm;
import com.scalar.forecast.salesPersonSetup.dao.SalesPersonSetupDAO;
import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import com.scalar.forecast.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/** Process the request to load sales person related details
 *
 * Created by Gandhi.
 * Date: Feb 8, 2005
 * Time: 7:00:25 PM
 */
public class SalesPersonSetupLoadSalesCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws ScalarException
    {

            SalesPersonSetupForm salesPersonSetupForm = (SalesPersonSetupForm)form;

            HttpSession session = request.getSession();
            HashMap info = new HashMap();
            HashMap errors = new HashMap();
            boolean success = false;
            int  salesPersonId = -1;
            DBTransaction dbTransaction = null;
            dbTransaction = DBTransaction.getInstance();
            if( dbTransaction != null )
            {
                dbTransaction.begin();
            }
            try
            {

                    salesPersonSetupForm.setExistingUserId( null );
                    salesPersonSetupForm.setForecastLevelOrder( -1 );
                    salesPersonSetupForm.setManagerId( -1 );

                    salesPersonId = salesPersonSetupForm.getExistingSalesPersonId();
                    int levelOrder = SalesPersonSetupDAO.getLevelOrder(salesPersonId) + 1;
                    SalesPersonSetupDAO.getSalesPerson( null , salesPersonSetupForm , salesPersonId );

                    request.setAttribute("salesPersonList", SalesPersonSetupDAO.getSalesPersonList());
                    request.setAttribute("userList", SalesPersonSetupDAO.getUserList());
                    request.setAttribute("levelList", SalesPersonSetupDAO.getLevelList());
                    request.setAttribute("mgrList", SalesPersonSetupDAO.getManagerList(levelOrder, salesPersonSetupForm.getExistingSalesPersonId ()));
                    ArrayList salesPersonsList = SalesPersonSetupDAO.loadSalesPersonsList( null );
                    if ( salesPersonsList.size() > 0  )
                    {
                        request.setAttribute("showList", "true");
                        request.setAttribute("salesPersonsList",  salesPersonsList );
                    }

            }

            catch(ScalarException e)
            {
                errors.put ("Could not load sales person info from data base", null);
                request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);

                e.printStackTrace();
            }
            finally
            {
                // Rollback or Commit the Transaction
                try
                    {
                        if (success == true) {
                            dbTransaction.commit();
                        }else{
                            dbTransaction.rollback();
                        }
                    }
                    catch (DBXException e)
                    {
                        e.printStackTrace();
                    }
            }


            ActionForward actionForward = mapping.findForward("success");
            return actionForward;
    }
}
